%% this shows the mean PSNR,and iteration
clc; clear;
close all;
data_path = srpath.getDataPath;
result_path = srpath.getResultPath;

%% set caffemodel and deploy file path !!!!
net_folder = '[VDSR0-1]-tuning2-[VDSR-r-1]';
net_file = 'VDSR-r_deploy.prototxt';
model_path = fullfile(result_path, net_folder, 'models');  % choose your caffemodel
net_model =  fullfile(result_path, net_folder, net_file);  % choose your deploy file 

%%
img_ext = '.bmp';

%%
% test_data = fullfile(data_path, 'Set14');
% test_data = fullfile(data_path, 'Set5');
test_data = fullfile(data_path, 'mytest');
% test_data = fullfile(data_path, 'General100');
% test_data = fullfile(data_path, 'B100');

%%
up_scale = 3;
prex = 'VDSR-r_iter_';
%prex = '_iter_';
init = 100;
interval = 100; % snapshot % ??????
offset = init;

%% we should do some preparation,include our caffemodel
model_files = dir(fullfile(model_path, '*.caffemodel'));
model_files = cell(1, length(model_files));
for countmodel = 1 : length(model_files)
    model_files{countmodel} = fullfile(model_path, [prex, num2str(init) '.caffemodel']);
    init = init+interval;
end
disp('All models are already');

%%
img_files = dir(fullfile(test_data, ['*' img_ext]));
%filemodelpaths = dir(fullfile(caffepa,'*.caffemodel'));
psnr_bic = zeros(1, length(img_files));
psnr_decon = zeros(1, length(img_files));

psnr_iter_decon = zeros(1, length(model_files)); %shows the relationship between psnr and iteration!!!!!
psnr_iter_bic = zeros(1, length(model_files)); %shows the relationship between psnr and iteration!!!!!
psnr_iter_goal = 28 * ones(1, length(model_files));  %!!!!!!111this is the result that we pursuit

mycaffe.reset_all();
caffe.set_mode_gpu();
caffe.set_device(1);

phase = 'test'; % run with phase test (so that dropout isn't applied)

for file_i = 1 : length(model_files)
    fprintf('every model costs time as follows\n');
    
    %net_weights = fullfile(caffepa,fimymdnamelemodelpaths(cafpath).name);
    net_weights = model_files{file_i};
    % net_weights = 'caffemodel/devdsr_iter_1200.caffemodel';
    net = caffe.Net(net_model, net_weights, phase);
    
    for i = 1 : length(img_files)
        %% read ground truth image
        [add,imname,type] = fileparts(img_files(i).name); % ??????
        im = imread(fullfile(test_data, [imname type]));
        
        %% work on illuminance only
        if size(im,3) > 1
            im_ycbcr = rgb2ycbcr(im);
            im = im_ycbcr(:, :, 1);
        end
        im_gnd = single(modcrop(im, up_scale)) / 255;
        
        %% bicubic interpolation
        im_l = imresize(im_gnd, 1/up_scale, 'bicubic');
        im_b = imresize(im_l, up_scale, 'bicubic');
        
        %% 2. Made myself.if  you want to use caffemodel for reconstruction, must cancel the commment
        [height, width, fmnum, num] = size(im_b);  % to gain myinput picture size,so we need not to change our prototxt evewty time
        net.blobs('data').reshape([height, width, fmnum, num]);
        net.reshape();
        net.forward({im_b});
        
        % im_h = net.blobs('sum').get_data();
        im_h = im_b + net.blobs('conv20').get_data();
        
        %%  remove border, the  border is very important ,the scale !!!下面的uint8是必须的，才能打印imshow
        im_h    = shave(uint8(im_h * 255), [up_scale, up_scale]);
        im_gnd  = shave(uint8(im_gnd * 255), [up_scale, up_scale]);
        im_b    = shave(uint8(im_b * 255), [up_scale, up_scale]);
        
        %% compute PSNR
        psnr_bic(i) = compute_psnr(im_gnd, im_b);
        psnr_decon(i) = compute_psnr(im_gnd, im_h);
        
        %% save results
        mycaffe.reset_all();%!!!!! this is very important!!!!!!
    end
    psnr_iter_bic(file_i) = mean(psnr_bic);
    psnr_iter_decon(file_i) = mean(psnr_decon);
    psnr_iter_decon(file_i) 
    
end

%% Finally,we draw the relationship between iteration(caffemodel) and psnr
%figure;plot(psnr_iter_srcnn-clip,'r');hold on;plot(psnr_iter_bic,'--g');hold on;plot(psnr_iter_goal,'b');xlabel('srcnn-clip-iteration');ylabel('srcnn-clip-psnr');title('this my srcnn-clip');legend('psnr-iter-srcnn-clip','psnr-iter-bic','psnr-iter-goal');
figure;
plot(psnr_iter_decon, 'r'); hold on;
plot(psnr_iter_bic, '--g'); hold on;
plot(psnr_iter_goal, 'b');
xlabel('iteration'); ylabel('mysrcnn-psnr'); title('this my srcnn');legend('psnr-iter-srcnn','psnr-iter-bic','psnr-iter-goal');
%this a base line
psnr_iter_goal1 = 29*ones(length(model_files),1);
hold on;plot(psnr_iter_goal1);

%% the following is to help us annalyse
fprintf('\n\nthe following is our analyse reporter \n');
MAX = max(psnr_iter_decon);
MAXPLA = find(psnr_iter_decon == MAX);
MIN = min(psnr_iter_decon);
MEAN = mean(psnr_iter_decon);
OFFSET = offset + (MAXPLA-1) * interval;
fprintf('\n ******************************\n');
fprintf('MAX =  %f, MIN = %f, MEAN =  %f, ...caffemodel number is %d \n  ******************************\n',MAX, MIN, MEAN, OFFSET);
fprintf('bic is  %f   *******************************\n', mean(psnr_iter_bic));
